<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>单位换算</title>
    <link href="../../rs/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="../../rs/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="../../rs/plugins/bootstrap_tab/css/bootstrap-table.css" rel="stylesheet">
    <link href="../../rs/css/animate.min.css" rel="stylesheet">
    <link href="../../rs/plugins/bootstrap_tab/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="../../rs/css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <link href="../../rs/plugins/datapicker/datepicker3.css" rel="stylesheet">
    <link href="../../rs/plugins/bootstrap-editable/css/bootstrap-editable.css" rel="stylesheet">
    <?php
         require_once('login.php');
    ?>
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <!-- Panel Other -->
    <div class="ibox float-e-margins">
       <div class="ibox-content">
            <div class="row" style="padding-bottom:10px">
                <div class="col-sm-8" >
                    <button style="  margin-right:5px"  id="remove" class="btn btn-danger" disabled>
                        <i class="glyphicon glyphicon-trash"></i>
                    </button>
                    <button onclick="add_btn()" style=" margin-left:5px;" class="btn ">
                        <i class="glyphicon  glyphicon-plus"></i> 新增
                    </button>
                </div>
                <div class="col-sm-4">
                    <div class="input-group" style="width:300px;float:right;"  id="key">
                        <input style="height:34px;" id="searchtext" type="text" placeholder="请输入关键词" class="input-sm form-control">
                        <span class="input-group-btn">
                          <button onclick="do_search()" style="height:34px;" type="button" class="btn btn-sm "> 查找</button>
                        </span>
                    </div>
                </div>
            </div>
            <table id="table"></table>
        <!-- End Example Card View -->
    </div>
</div>
</div>
<!-- End Panel Other -->


<div style="display: none" id="open_edit">
    <form id="edit_from">
        <div class="input-group"   style="padding: 20px 20px 10px 20px;">
            <div class="input-group-btn">
                <button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">名&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称</button>
            </div>
            <input type="text" class="form-control "  name="hsmc" >
        </div>
        <div class="input-group"   style="padding: 0px 20px 10px 20px;">
            <div class="input-group-btn">
                <button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">基本单位</button>
            </div>
            <select class="form-control" id="dwid"></select>
        </div>
        <div class="input-group"   style="padding: 0px 20px 10px 20px;">
            <div class="input-group-btn">
                <button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">换算比例</button>
            </div>
            <input type="text" class="form-control "   name="hsbl">
        </div>
        <div class="input-group"   style="padding: 0px 20px 10px 20px;">
            <div class="input-group-btn">
                <button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注</button>
            </div>
            <input type="text" class="form-control "   name="bz">
        </div>
        <div class="form-group"   >
            <div class="col-sm-11" style="padding: 10px 20px 10px 20px;">
                <label class="checkbox-inline i-checks"><input type="checkbox"    name="zt">是否停用</label>
            </div>
        </div>
        <div style="position: absolute;width:100%;bottom:0px;padding: 0px 20px 10px 20px;">
            <button type="reset" id="reset" style="display:none;">reset</button>
            <button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown"
                    style="background-color: #e2e2e2;" onclick="layer.closeAll()">返回
            </button>
            <button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown"
                    style="background-color: #f54141;color: white;float: right" id="save_do">保存
            </button>
        </div>
    </form>
</div>

<script src="../../rs/js/jquery.min.js"></script>
<script src="../../rs/js/bootstrap.js"></script>
<script src="../../rs/plugins/bootstrap_tab/js/bootstrap-table.min.js"></script>
<script src="../../rs/plugins/bootstrap_tab/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="../../rs/plugins/datapicker/bootstrap-datepicker.js"></script>
<script src="../../rs/js/jquery.cookie.js"></script>
<script src="../../rs/js/common_qj.js"></script>
<script src="../../rs/plugins/layer/layer.min.js"></script>
<script src="../../rs/plugins/bootstrap-editable/js/bootstrap-editable.js"></script>
<script src="../../rs/plugins/bootstrap_tab/js/plugins/iCheck/icheck.min.js"></script>
<script src="../../rs/plugins/bootstrap_tab/js/plugins/layer/laydate/laydate.js"></script>
<script src="../../rs/plugins/bootstrap-editable/js/bootstrap-table-editable.min.js"></script>
<script type="text/javascript">
     $(document).ready(function(){$(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",})});
    var sign = URLencode($.cookie('sign'));
    var $table = $('#table');
    var $remove = $('#remove');
    var mydata;
    $(function(){
         $('#table').bootstrapTable({
            columns: [
                {
                    field: 'state',
                    checkbox: true
                },
                {
                    field: 'rid',
                    title: '序号',
                    align: 'left',
                    formatter:"actionFormatter"
                },
                 {
                    field: 'hsmc',
                    align: 'left',
                    title: '单位名称'
                },
                 {
                    field: 'dwmc',
                    align: 'left',
                    title: '基本单位'
                },
                {
                    field: 'hsbl',
                    align: 'left',
                    title: '换算比例'
                },
                {
                    field: 'zt',
                    title: '是否停用',
                    align: 'left',
                    formatter:"actionFormatter1"
                },
                {
                    field: 'bz',
                    align: 'left',
                    title: '备注'
                }
                ,
                {
                    field: 'tips',
                    align: 'left',
                    title: '备注',
                    formatter:"actionFormatter2"
                }
            ],
            singleSelect: true,
            pagination: true,
            pageSize:20,
             
        });
        f_sheard();
        get_dw();
    });


    function f_sheard() {
    layer.load(2);
        $.ajax({
            type: "post",
            url:G_url+'unca/query/',
            data:'sign='+sign,
            dataType: "json",
            success: function (aftdata) {
            mydata=aftdata;
            layer.closeAll('loading');
            $table.bootstrapTable('load', aftdata.data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        });
    }


    $(function () {
        <!-- 删除操作 -->
        $table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
            $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);
        });
        $remove.click(function () {
            var rids = $.map($table.bootstrapTable('getSelections'), function (row) {
                return row.rid;
            });
            $.ajax({
                type: "post",
                url: G_url+'doi/del/unca/?sign='+sign,
                data:"&rid=" +rids,
                dataType: "json",
                success: function (result) {
                    if (result.ok) {
                        layer.closeAll();
                        $table.bootstrapTable('remove', {
                            field: 'rid',
                            values: rids
                        });
                        layer.msg(result.msg);

                    } else {
                        layer.msg(result.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    //alert(errorThrown);
                }
            });
            $remove.prop('disabled', true);
        });
        // <!-- 删除操作 -->

    });


    // <!--新增操作-->
    function add_btn(){
        $("#dwid").attr('disabled',false);
        open_modal();
    }
    function open_modal(){
        $("#open_edit").show();
        $("input[name='bh']").attr("disabled",false);
        $('#reset').click();
          $("input[name='zt']").iCheck('uncheck');
        layer.open({
            title: "新增",
            type: 1, 
            offset: 'rb',
            area: ['40%', '100%'], //宽高
            shadeClose: true,
            content: $('#edit_from')
        });
        
    }
    <!--新增操作-->

    <!--保存操作-->
    var rid;
    var index;
    $("#save_do").on("click",function(){
        var layer_title=$('.layui-layer-title').text();
        var hsmc=$.trim($("input[name='hsmc']").val());
        var dwid=$.trim($('#dwid').find("option:selected").val());
        var hsbl=$.trim($("input[name='hsbl']").val());
        var zt= $("input[name='zt']").is(':checked');
        if(zt){zt="1"}else{zt="0"};
 
        var bz=$("input[name='bz']").val();
       
        if(layer_title=="新增"){
            if (!f_valid()) return;
            $.ajax({
                type: "post",
                url:G_url+'doi/add/unca'+'?sign='+sign,
                data:"hsmc="+hsmc+"&dwid="+dwid+"&hsbl="+hsbl+"&zt="+zt+"&bz="+bz,
                dataType: "json",
                success: function (result){
                    if (result.ok){
                        f_sheard();
                        layer.msg(result.msg);
                    } else {
                        layer.msg(result.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                }
            });
            layer.closeAll();

        }else if(layer_title=="修改"){
            
            $.ajax({
                type: "post",
                url:G_url+'doi/edit/unca'+'?sign='+sign,
                data:"rid="+rid+"&hsmc="+hsmc+"&dwid="+dwid+"&hsbl="+hsbl+"&zt="+zt+"&bz="+bz,
                dataType: "json",
                success: function (result){
                    if (result.ok){
                        f_sheard();
                        layer.msg(result.msg);
                    }else{
                        layer.msg(result.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                }
            });
            layer.closeAll();
        }


    });

    // <!--保存操作-->

    // <!--修改信息-->
    function actionFormatter(value, row, index){
        return "<a class='label label-warning'  href='javascript:void(0)' onclick='open_edit("+JSON.stringify(row)+","+index+");'>"+value+"</a>";
    }
     function actionFormatter1(value, row, index){
         if (value==0){
             return "否"
         }else{
              return "是"
         }    
    }
     function actionFormatter2(value, row, index){
             return "1"+row.hsmc+"="+row.hsbl+row.dwmc;
    }
    function open_edit(r,i){
         
       
        var dwid=r.dwid;
        if (r){
            $("#dwid").find("option[value="+dwid+"]").prop("selected",true);
            $("#open_edit").show();
            
            rid= r.rid;
            index=i;
            var hsmc=$("input[name='hsmc']").val(r.hsmc);
            var hsbl=$("input[name='hsbl']").val(r.hsbl);
            var bz=$("input[name='bz']").val(r.bz);
            if(r.zt=="1"){
                $("input[name='zt']").iCheck('check');
            }else{
                $("input[name='zt']").iCheck('uncheck');
            }
           
            layer.open({
                title: "修改",
                type: 1,
                offset: 'rb',
                area: ['40%', '100%'], //宽高
                shadeClose: true,
                content: $('#edit_from')
            });
        }
         $("#dwid").attr('disabled',true);
    };
    <!--修改信息-->

    <!--本地搜索-->
    

    $('#searchtext').bind('keypress',function(event){
        if(event.keyCode == "13") {
            do_search();
        }
    });
    function do_search(){
        var gjc=$('#searchtext').val(); //搜索关键词
        if(gjc==""){
           
            $table.bootstrapTable('load', mydata);
        }else{
            var newdata=[];
            for(var i=0;i<mydata.data.length;i++){
                str=transform(mydata.data[i]).join(',');
                row=str.indexOf(gjc);
                if(row>=0){
                    newdata.push(mydata.data[i]);
                }
            }
            $table.bootstrapTable('load', newdata);
        }
    }
    function transform(obj){
        var arr = [];
        for(var item in obj){
            arr.push(obj[item]);
        }
        return arr;
    }
    // <!--本地搜索-->

    //获取单位
  function get_dw(){
    $.ajax({
      type: "post",
      url:G_url+'doi/see/unit',
      data:'sign='+sign,
      dataType: "json",
      success: function (result){
        //给门店编号做循环
        var ff = result.data;
        var html = '';
        $.each(ff, function (a, b) {//直接取出json的prolist做each循环.
          html += "<option value='"+b.rid+"'>" + b.dwmc + "</option>";//拼接字符串
        });
        $("#dwid").append(html);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
      }
    });
  }


  var f_valid = function(){
		var hsmc = $.trim($("input[name='hsmc']").val()) ;
		if( hsmc =='') {
			layer.msg('请输入名称');
			return false;
		}
		var hsbl = $.trim($("input[name='hsbl']").val()) ;
		if(hsbl =='') {
			layer.msg('请输入换算比例');
			return false;
		}
		 
		 
		return true;
	};

</script>
</body>
</html>
